/* 
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
function calTime(){
    var currentETime = new Date();
    var eTime = currentETime.getTime();
    $('#responseTime').html(eTime - sTime);
}
var sTime;
function startTime(){
    var currentSTime = new Date();
    sTime = currentSTime.getTime();
    $('#responseTime').html("<img src=\"./images/loader.gif\" />");
}

 
function rejectBo(hgabo1,shelf1){
    $.blockUI();
    $.post(
        "RoomSettingServlet",
        {
            action: "loadBoData",
            hgabo:hgabo1,
            shelf:shelf1
        },
        function(data){
            if(data.success){

                $("#hgabo").val(hgabo1);

                $("#qty").focus();
                
            }
            
        },
        "json"

        );
   
}
 function loadRoomInfoTable(){
      $.blockUI();
 $.post("LoadRoomTable",{
        action: "loadroominfotable"
    },
    function(data){

        $("#showTable").html(data);

    },
    "html"

    );
}
function loadshelfinfotable(shelf1){
    $.post(
        "LoadRoomTable",
        {
            action: "loadshelfrejecttable",
            edit: "Y",
            shelf: shelf1
        },
        function(data){
            $("#shelfinfotable").html(data)
        },
        "html"

        );
}
function loadRoom(){
    $.blockUI();
    $.post("LoadRoomTable",{
        action: "loadroom"
    },
    function(data){

        for( i in data )
        {

            $('#'+i).attr("src", data[i]);


        }

    },
    "json"

    );
        $.blockUI();
   $.post("LoadRoomTable",{
        action: "loadroomqty"
    },
    function(data){

        for( i in data )
        {

            $('#QTY'+i).text(data[i]);


        }

    },
    "json"

    );
     //เรียกใช้ BlockUI
}

function  loadRoomInfo(){
    $.blockUI();
    $.post("LoadRoomTable",{
        action: "loadroominfo",
        shelf:$("#shelf").val()
    },
    function(data){
        if(data.success){
           
            $("#productname").text(data.productname+"-"+data.tgatype);
            //$("#tgatype").val(data.tgatype);
            //$("bondpad").val(data.bondpad);
            $('#room img').each(function()
            {
                $(this).attr('class', '');
            });
            $("#"+data.shelfno).attr('class', 'rollover_img');

        }else{
            alert("can't load shelf data");
            $("#shelf").val("");
            $("#productname").text("");
            $("#hgabo").val("");
            $("#qty").val("");
        }

    },
    "json"

    );
    
}
function checkshelfcap(shelf1,qty1){
    $.blockUI();
    $.post(
        "RoomSettingServlet",
        {
            action: "checkshelfcap",
            shelf: shelf1 ,
            qty:qty1
        },
        function(data){
            return  data.success;

        },
        "json"

        );
    
}
function checkhgaboduplicate(shelf1,hgabo1){
    $.blockUI();
    $.post(
        "RoomSettingServlet",
        {
            action: "checkhgaboduplicate",
            shelf: shelf1 ,
            hgabo:hgabo1
        },
        function(data){
            return  data.success;
        },
        "json"

        );
    
}
function findBoData(hgabo1){
    $.blockUI();
    $.post(
        "RoomSettingServlet",
        {
            action: "findBoData",
            hgabo:hgabo1
        },
        function(data){
            return  data;
        },
        "json"

        );
    
}
function updateHGABOQty(shelf1,hgabo1,qty1,action1){

    $.post(
        "RoomSettingServlet",
        {
            action: action1,
            shelf: shelf1 ,
            hgabo:hgabo1,
            qty:qty1
        },
        function(data){
            if(data.success){
                alert("Success to add data");
                loadRoom();
                $("#shelf").val("");
                $("#productname").text("");
                $("#hgabo").val("");
                $("#qty").val("");
            }else{
                alert("Error!! can't add data");
            }
        },
        "json"

        );

}
function addHGABOQty(shelf1,hgabo1,tsr1,partnum1,tab1,qty1){

    $.post(
        "RoomSettingServlet",
        {
            action: "addshelfbo",
            shelf: shelf1 ,
            hgabo:hgabo1,
            qty:qty1,
            tsr:tsr1,
            partnum:partnum1,
            tab:tab1
        },
        function(data){
            if(data.success){
                alert("Success to add data");
                loadRoom();
                $("#shelf").val("");
                $("#productname").text("");
                $("#hgabo").val("");
                $("#qty").val("");
            }else{
                alert("Error!! can't add data");
            }
        },
        "json"

        );
     
}

$(document).ready(function()
{
    loadRoom();
    // Make sure to only match links to wikipedia with a rel tag
    $('#room img').each(function()
    {
        // We make use of the .each() loop to gain access to each element via the "this" keyword...
        $(this).qtip(
        {
            content: {
                text: 'Loading...', // The text to use whilst the AJAX request is loading
                ajax: {
                    url: 'LoadRoomTable', // URL to the local file
                    type: 'POST', // POST or GET
                    data: "action=loadroomtooltip&shelf="+$(this).attr('id'), // Data to pass along with your request
                    success: function(data, status) {
                        // Process the data

                        // Set the content manually (required!)
                        this.set('content.text', data);
                    }
                }
            },
            position: {
                my: 'left center',  // Position my top left...
                at: 'right center', // at the bottom right of...
                adjust: {
                    screen: true
                } // Keep the tooltip on-screen at all times
            },
            style: {
                classes: "ui-tooltip-dark"

            }

        })


    });
    $('#room img').each(function()
    {
        $(this).click(function() {
            $('#shelfinfotable').html("<img src=\"./images/loader.gif\" />");
            $('#room img').each(function()
            {
                $(this).attr('class', '');
            });
            $(this).attr('class', 'rollover_img');
            $("#shelf").val($(this).attr('id'));
            $.post(
                "LoadRoomTable",
                {
                    action: "loadshelfrejecttable",
                    edit: "Y",
                    shelf: $(this).attr('id')
                },
                function(data){
                    $("#shelfinfotable").html(data)
                },
                "html"

                );
        //loadRoomInfo();
          
        });
    });


    jQuery("#inputform").validationEngine('attach', {
        onValidationComplete: function(form, status){
            if(status){
                $.blockUI();
                //var shelf1=$("#shelf").val();

                var hgabo1 = $("#hgabo").val();
                var qty1 = $("#qty").val();

                $.post(
                    "RoomSettingServlet",
                    {
                        action: "checkBoData",
                        hgabo: hgabo1
                    },
                    function(data){
                        if(data.success){
                            if((data.qty*1)<(qty1*1)){
                                alert("You can't reject HGA BO more than "+data.qty +". Please try again");
                            }else{
                                if( confirm("Do you want to reject HGA BO:"+hgabo1 +" \n Quantity ="+qty1)){
                                   $.blockUI();
                                    $.post(
                                        "RoomSettingServlet",
                                        {
                                            action: "rejecthagboqty",
                                            hgabo:hgabo1,
                                            qty:qty1
                                                
                                        },
                                        function(data){
                                           
                                            if(data.success){

                                                alert("Success to add reject HGA BO quantity");
                                                loadRoom();
                                                $("#hgabo").val("");
                                                $("#qty").val("");
                                                loadshelfinfotable(data.shelf);
                                            }else{
                                                alert("Error!! can't add data");
                                            }
                                            
                                        },
                                        "json"

                                        );
                                }
                            }

                        }else{
                            alert("Error can't load HGA BO data");
                        }
                        
                    },
                    "json"

                    );




                
            }
        }
    });

//    $('#hgabo').blur(function() {
//        var hgadata=$('#hgabo').val();
//        if(hgadata!="" ){
//            $.blockUI();
//            $.post(
//                "RoomSettingServlet",
//                {
//                    action: "findBoData",
//                    hgabo:hgadata
//                },
//                function(data){
//                    if(data.success){
//                        $("#tsr").val(data.tsr);
//                        $("#tab").val(data.tab);
//                        $("#partnum").val(data.partnum);
//                    }else{
//                        alert("Can't find HGA BO NAME:\""+hgadata+"\" in ETS ");
//                    }
//                    
//                },
//                "json"
//
//                );
//
//        }
//
//
//    });
                 
});